package com.xuecheng.content.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.domain.mapper.CourseBaseMapper;
import com.xuecheng.content.domain.model.dto.CourseBaseMarketDto;
import com.xuecheng.content.domain.model.dto.CoursePreviewDto;
import com.xuecheng.content.domain.model.dto.TeachplanDto;
import com.xuecheng.content.domain.service.CourseBaseService;
import com.xuecheng.content.domain.model.entity.CourseBase;
import com.xuecheng.content.domain.service.CourseMarketService;
import com.xuecheng.content.domain.service.TeachplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-07-26  13:28
 */
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {

    @Autowired
    private CourseBaseMapper courseBaseMapper;

    @Autowired
    private CourseMarketService courseMarketService;

    @Autowired
    private TeachplanService teachplanService;

    @Override
    public PageResult<CourseBase> queryCourseBasePage(LambdaQueryWrapper<CourseBase> queryWrapper, Page<CourseBase> page) {
        // 查询数据内容获得结果
        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, queryWrapper);
        // 获取数据列表
        List<CourseBase> list = pageResult.getRecords();
        // 获取数据总数
        long total = pageResult.getTotal();
        // 构建结果集
        return new PageResult<>(list, total, page.getCurrent(), page.getSize());
    }

    @Override
    public CourseBase createCourseBase(CourseBase courseBase) {
        int insert = courseBaseMapper.insert(courseBase);
        if (insert <= 0){
            throw new XueChengPlusException("新增课程基本信息失败");
        }
        return courseBase;
    }

    @Override
    public CoursePreviewDto queryCourseBasePreview(Long courseId) {
        CourseBaseMarketDto courseBaseMarketDto = courseMarketService.queryCourseBaseMarket(courseId);
        List<TeachplanDto> teachplanDtos = teachplanService.queryTreeNodes(courseId);
        return new CoursePreviewDto(courseBaseMarketDto, teachplanDtos);
    }
}
